package org.groovyflow.spring.locator;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;


public class SingletonBeanLocator implements BeanFactoryAware {
	
	private static SingletonBeanLocator instance;
	private BeanFactory beanFactory;
	
	private SingletonBeanLocator(){
	}
	

	public synchronized static SingletonBeanLocator getInstance(){
		if(instance == null)
			instance = new SingletonBeanLocator();
		return instance;
	}
	
	public Object getBean(String beanName){
		return this.beanFactory.getBean(beanName);
	}
	
	/**
	 * Because we're BeanFactoryAware, we expect Spring to inject
	 * a BeanFactory into us.
	 */
	public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
		System.out.println("beanFactory has been set into " + this.getClass().getName());
		this.beanFactory = beanFactory;
	}
	
}
